//
//  ChangeNameViewController.m
//  OneWordNew
//
//  Created by qianfeng001 on 15-7-8.
//  Copyright (c) 2015年 朱共营. All rights reserved.
//

#import "ChangeNameViewController.h"
#import "AFNetworking.h"
@interface ChangeNameViewController ()
{
    AFHTTPRequestOperationManager *_manager;
}

@end

@implementation ChangeNameViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _manager=[AFHTTPRequestOperationManager manager];
    _manager.responseSerializer=[AFHTTPResponseSerializer serializer];
    
}
- (void)showAliertWithTitle:(NSString *)title message:(NSString *)message{
    UIAlertView *alert=[[UIAlertView alloc] initWithTitle:title message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil];
    [alert show];
    
}
- (void)changeNameWithUrl:(NSString *)url{
    __weak typeof(self)weakSelf=self;
    NSString *myurl=[url stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSString *token=[[NSUserDefaults standardUserDefaults] objectForKey:@"token"];
    NSString *newName=[self.nameLable.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
    NSDictionary *dict=@{@"token":token,
                         @"key":@"3a4b2a12539a916c040d069ae8ac8310",
                         @"nickname":newName
                         };
    [_manager POST:myurl parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        if (responseObject) {
            NSDictionary *dict=[NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
            if ([dict[@"code"] isEqual:@(0)]) {
                [weakSelf showAliertWithTitle:@"提示" message:dict[@"msg"]];
                NSUserDefaults *ud=[NSUserDefaults standardUserDefaults] ;
                [ud setObject:self.nameLable.text forKey:@"nickname"];
                [ud synchronize];
                
                
            }else{
                
                [weakSelf showAliertWithTitle:@"提示" message:dict[@"msg"]];
            }
            
            
        }
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [ self showAliertWithTitle:@"提示" message:@"错误"];
    }];
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


- (IBAction)pushBtnClick:(UIButton *)sender {
    [self changeNameWithUrl:kChangeNameUrl];
}
@end
